package com.wlf.admin.common.dto;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.wlf.admin.system.domain.SysMenu;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 初始化装载类
 */
@Getter
@Setter
public class LayuiMiniDto {

    /**
     * 主页信息
     */
    private HomeInfo homeInfo = new HomeInfo();

    /**
     * Logo信息
     */
    private LogoInfo logoInfo = new LogoInfo();

    private List<Tree<String>> menuInfo;


    public static LayuiMiniDto handleMenu(List<SysMenu> menus) {
        LayuiMiniDto layuiMiniDto = new LayuiMiniDto();
        TreeNodeConfig tConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        tConfig.setIdKey("id");
        tConfig.setChildrenKey("child");
        tConfig.setNameKey("title");

        layuiMiniDto.menuInfo = TreeUtil
                .build(menus.stream().filter(i -> i.getType().equals(Const.MenuType.menu.name()))
                                .collect(Collectors.toList()), "0", tConfig,
                        (treeNode, tree) -> {
                            tree.setId(treeNode.getId().toString());
                            tree.setParentId(treeNode.getPid().toString());
                            tree.setWeight(treeNode.getSort());
                            tree.setName(treeNode.getName());
                            tree.putExtra("href", treeNode.getUrl());
                            tree.putExtra("auth", treeNode.getAuth());
                            tree.putExtra("icon", treeNode.getMenuIcon());
                            tree.putExtra("target", treeNode.getTarget());
                        });
        return layuiMiniDto;
    }

    @Getter
    @Setter
    static
    class HomeInfo {
        private String title = "首页";
        private String href = "main";
    }

    @Getter
    @Setter
    static
    class LogoInfo {
        private String title = "MyAdmin";
        private String image = "static/images/logo.png";
        private String href = "";
    }
}
